﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;

namespace QuanLyQuanCaPhe
{
    public partial class frmQuanLyCaPhe : Form
    {
        private int soBan;
        private string ID;
        public frmQuanLyCaPhe(string _soBan, Boolean ChuQuan, string _ID)
        {
            InitializeComponent();
            soBan = Convert.ToInt32(_soBan.Substring(0,2));
            ID = _ID;
            if (!ChuQuan)
            {
                tùyChọnToolStripMenuItem.Visible = thốngKêToolStripMenuItem.Visible = tàiKhoảnToolStripMenuItem.Visible = nhậpHàngToolStripMenuItem.Visible = hàngHóaToolStripMenuItem.Visible = false;
            }
        }

        private void QuanLyCaPhe_Load(object sender, EventArgs e)
        {
            hienThi();
        }

        private void hienThi()
        {
            listTable.Clear();
            for (int i = 1; i <= soBan; i++)
            {
                string tenban = "";
                if (i < 10)
                    tenban = "0" + i;
                else
                    tenban = "" + i;

                listTable.LargeImageList = imageList;
                /*string sql = "select * from goimon where banso = " + System.Convert.ToInt32(tenban);
                /DataSet ds = connection_sql.FillDataSet(sql);

                if (ds.Tables[0].Rows.Count > 0)
                    listTable.Items.Add(tenban, 1);
                else*/
                listTable.Items.Add(tenban, 0);

            }
        }

        private void listTable_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string soBan = listTable.SelectedItems[0].SubItems[0].Text.Trim();
            hienThiBan(soBan, listTable.SelectedItems[0].Index);
        }

        private void hienThiBan(string soBan,int index)
        {
            /*frmQuanLyBan frmQLB = new frmQuanLyBan(soBan, ID);
            frmQLB.ShowDialog();
            string sql = "select * from goimon where banso = " + System.Convert.ToInt32(soBan);
            DataSet ds = connection_sql.FillDataSet(sql);
            if (ds.Tables[0].Rows.Count > 0)
                listTable.Items[index].ImageIndex = 1;
            else
                listTable.Items[index].ImageIndex = 0;*/
        }

        private void menuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //frmMenu frmMenu = new frmMenu();
            //frmMenu.ShowDialog();
        }

        private void bànToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //frmTable frmTable = new frmTable();
            //frmTable.ShowDialog();
        }

        private void picSearch_Click(object sender, EventArgs e)
        {
            /*if (!(ban > listTable.Items.Count))
            {
                string tenban = "";
                if ((ban / 100) < 9)
                    tenban = "0" + ban;
                else
                    tenban = "" + ban;
                hienThiBan(tenban);
            }*/
            try
            {
                string tenBan = "";
                if (txtSoBan.Text.Trim().Length == 3)
                    tenBan = "0" + txtSoBan.Text.Trim();
                else
                    tenBan = txtSoBan.Text.Trim();

                for (int i = 0; i < listTable.Items.Count; i++)
                {
                    if (listTable.Items[i].Text == tenBan)
                    {
                        hienThiBan(tenBan, i);
                        break;
                    }
                }
            }
            catch { }
            txtSoBan.Text = "";
        }

        private void txtSoBan_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (System.Convert.ToInt16(txtSoBan.Text.Trim()) > 0 && txtSoBan.Text.Trim().Length > 2)
                {
                    picSearch.Enabled = true;
                    lblThongBao.Text = "";
                }
                else
                {
                    picSearch.Enabled = false;
                    lblThongBao.Text = "Nhập sai";
                }
            }
            catch 
            {
                if (txtSoBan.Text.Trim() != "")
                    lblThongBao.Text = "Nhập sai";
            }
        }

        private void txtSoBan_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (ASCIIEncoding.ASCII.GetBytes(e.KeyChar.ToString())[0] == 13)
                    picSearch_Click(sender, e);
            }
            catch { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //frmHoaDon frmHD = new frmHoaDon(System.Convert.ToInt32(1), "00:00");
            //frmHD.Show();
        }

        private void phụThuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //frmPhuThu frmPhuThu = new frmPhuThu();
            //frmPhuThu.ShowDialog();
        }

        private void frmQuanLyCaPhe_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn cập nhật lại hàng hóa trong kho trước khi thoát không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                /*frmKhoHang frmKH = new frmKhoHang();
                frmKH.ShowDialog();*/
                //frmKhoHangSub frmKHS = new frmKhoHangSub();
                //frmKHS.ShowDialog();
            }
        }

        private void khoHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //frmKhoHang frmKH = new frmKhoHang();
            //frmKH.Show();
        }

        private void tàiKhoảnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //frmUsers frmU = new frmUsers();
            //frmU.ShowDialog();
        }

        private void nhậpHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //frmNhapHang frmNH = new frmNhapHang();
            //frmNH.Show();
        }

        private void listTable_MouseClick(object sender, MouseEventArgs e)
        {
            if (listTable.SelectedItems.Count == 1)
            {
                
                if (e.Button == MouseButtons.Right)
                {

                    ContextMenu m = new ContextMenu();
                    m.MenuItems.Add(new MenuItem("Gọi món",menu_GoiMon));
                    if (listTable.SelectedItems[0].ImageIndex == 1)
                    {
                        m.MenuItems.Add(new MenuItem("Thanh toán", menu_ThanhToan));
                        m.MenuItems.Add(new MenuItem("Chuyển bàn", menu_ChuyenBan));
                    }
                    m.Show(listTable, new Point(e.X, e.Y));
                }
            }
        }

        private void menu_GoiMon(object sender, EventArgs e)
        {
            string soBan = listTable.SelectedItems[0].SubItems[0].Text.Trim();
            hienThiBan(soBan, listTable.SelectedItems[0].Index);
        }

        private void menu_ThanhToan(object sender, EventArgs e)
        {
            string soBan = listTable.SelectedItems[0].SubItems[0].Text.Trim();
            //frmQuanLyBan frmQLB = new frmQuanLyBan(soBan, ID);
            //frmQLB.hienThi();
            //frmQLB.btnThanhToan_Click(sender, e);
            hienThi();
        }

        private void menu_ChuyenBan(object sender, EventArgs e)
        {
            try
            {
                int banCanChuyen = System.Convert.ToInt32(listTable.SelectedItems[0].SubItems[0].Text.Trim());
                int tongBanKhongSuDung = 0;
                for (int i = 0; i < listTable.Items.Count; i++)
                {
                    if (listTable.Items[i].ImageIndex == 0)
                        tongBanKhongSuDung++;
                }
                string[] danhSachBan = new string[tongBanKhongSuDung];

                int j = 0;
                for (int i = 0; i < listTable.Items.Count; i++)
                {
                    if (listTable.Items[i].ImageIndex == 0)
                    {
                        danhSachBan[j] = listTable.Items[i].Text;
                        j++;
                    }
                }
                listTable.SelectedItems[0].ImageIndex = 0;
                //frmChuyenBan frmCB = new frmChuyenBan(banCanChuyen, danhSachBan);
                //frmCB.ShowDialog();
                hienThi();
            }
            catch (Exception ex) { MessageBox.Show(ex + ""); }
        }

        private void bánHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //frmTkBanHang frmtkbh = new frmTkBanHang();
            //frmtkbh.Show();
        }

        private void thốngKêNhậpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //frmTkNhapHang frmtknh = new frmTkNhapHang();
            //frmtknh.Show();
        }

        private void thốngKêToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //frmTkTonKho frmtktk = new frmTkTonKho();
            //frmtktk.Show();
        }

        private void hóaĐơnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //frmCTHoaDon frmcthd = new frmCTHoaDon();
            //frmcthd.Show();
        }

        private void thôngTinToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //frmAbout frmA = new frmAbout();
            //frmA.Show();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            hienThi();
        }
    }
}
